package com.whnk.web.controller.pm;

import java.text.ParseException;
import java.util.List;

import com.whnk.pm.dto.CheckDateDurationDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whnk.common.annotation.Log;
import com.whnk.common.base.AjaxResult;
import com.whnk.common.enums.BusinessType;
import com.whnk.framework.web.base.BaseController;
import com.whnk.framework.web.page.TableDataInfo;
import com.whnk.pm.domain.BaseInfo;
import com.whnk.pm.domain.ProjInfo;
import com.whnk.pm.service.IBaseInfoService;
import com.whnk.pm.service.IProjInfoService;

/**
 * 项目经历 信息操作处理
 * 
 * @author whwhnk
 * @date 2019-06-13
 */
@Controller
@RequestMapping("/pm/projInfo")
public class ProjInfoController extends BaseController
{
    private String prefix = "pm/projInfo";
	
	@Autowired
	private IProjInfoService projInfoService;
	
	@Autowired
	private IBaseInfoService baseInfoService;
	
	@RequiresPermissions("pm:projInfo:view")
	@GetMapping()
	public String projInfo()
	{
	    return prefix + "/projInfo";
	}
	
	/**
	 * 查询项目经历列表
	 */
	@RequiresPermissions("pm:projInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjInfo projInfo)
	{
		startPage();
        List<ProjInfo> list = projInfoService.selectProjInfoList(projInfo);
		return getDataTable(list);
	}
	
	/**
	 * 新增项目经历
	 */
	@GetMapping("/add/{empId}")
	public String add(@PathVariable("empId") String empId, ModelMap mmap) {
		BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
		mmap.put("baseInfo", baseInfo);
		return prefix + "/add";
	}
	
	/**
	 * 新增保存项目经历
	 */
	@RequiresPermissions("pm:projInfo:add")
	@Log(title = "项目经历", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProjInfo projInfo)
	{		
		return toAjax(projInfoService.insertProjInfo(projInfo));
	}

	/**
	 * 修改项目经历
	 */
	@GetMapping("/edit/{projId}")
	public String edit(@PathVariable("projId") Integer projId, ModelMap mmap)
	{
		ProjInfo projInfo = projInfoService.selectProjInfoById(projId);
		mmap.put("projInfo", projInfo);
	    return prefix + "/edit";
	}

	/**
	 * 修改项目经历
	 */
	@RequiresPermissions("pm:projInfo:edit:last")
	@GetMapping("/editLast/{projId}")
	public String editLast(@PathVariable("projId") Integer projId, ModelMap mmap)
	{
		ProjInfo projInfo = projInfoService.selectProjInfoById(projId);
		projInfoService.lastOneCheck(projInfo.getEmpId(), projId);
		mmap.put("projInfo", projInfo);
		return prefix + "/edit-last";
	}
	
	/**
	 * 修改保存项目经历
	 */
	@RequiresPermissions("pm:projInfo:edit")
	@Log(title = "项目经历", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProjInfo projInfo)
	{		
		return toAjax(projInfoService.updateProjInfo(projInfo));
	}
	
	/**
	 * 删除项目经历
	 */
	@RequiresPermissions("pm:projInfo:remove")
	@Log(title = "项目经历", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(projInfoService.deleteProjInfoByIds(ids));
	}
	
	/**
	 * 查看详细信息
	 * @param projId
	 * @param mmap
	 * @return
	 */
	@RequiresPermissions("pm:projInfo:detail")
    @GetMapping("/detail/{projId}")
    public String detail(@PathVariable("projId") Integer projId, ModelMap mmap)
    {
		ProjInfo projInfo = projInfoService.selectProjInfoById(projId);
		mmap.put("projInfo", projInfo);
        return prefix + "/detail";
    }

	@PostMapping("/checkDate")
	@ResponseBody
	public String checkDuration(CheckDateDurationDTO checkDateDurationDTO) throws ParseException {
		return projInfoService.checkDate(checkDateDurationDTO.getStartDate(),checkDateDurationDTO.getEndDate(),checkDateDurationDTO.getEmpId(), checkDateDurationDTO.getOperationType(), checkDateDurationDTO.getId());
	}
}
